package com.zyx.spring5msb.aoptx.service;

import com.zyx.spring5msb.aoptx.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Yaxi.Zhang
 * @since 2021/9/30 10:14
 */
@Service
public class BookService {
    @Autowired
    BookDao bookDao;

    /**
     * 结账： 传入哪个用户买了哪本书
     */
    /*@Transactional(propagation = Propagation.REQUIRED)*/
    @Transactional(propagation = Propagation.NESTED)
    public void checkout(String username, int id) {
        bookDao.updateStock(id);
        int price = bookDao.getPrice(id);
        bookDao.updateBalance(username, price);
        // int i = 1 / 0;
        // new FileInputStream("aaa.txt");
    }

    @Transactional
    public void updatePrice(int id, int price) {
        bookDao.updatePrice(id, price);
    }

}
